﻿Imports System.Globalization

Partial Public Class E_Pay
    Inherits System.Web.UI.Page

    Dim clsGet As New ClassFunctionGetData
    Dim clsFc As New ClassFunction
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not (IsPostBack) Then
            If Not (Session("PayinBooking") Is Nothing) Then
                Dim dt As DataTable = clsGet.Get_MatFor_Sale_Order(Session("PayinBooking"), CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("Customer_no"))
                ViewState("dt") = dt
                Dim Vat As String = ""
                Dim dsConfig As DataSet = Session("Config")
                If (dsConfig.Tables(0).Rows(0).Item("Vat").ToString().Split(".")(1) = "00") Then
                    Vat = CInt(dsConfig.Tables(0).Rows(0).Item("Vat").ToString().Split(".")(0)).ToString()
                Else
                    Vat = dsConfig.Tables(0).Rows(0).Item("Vat").ToString()
                End If
                ViewState("VAT") = Vat
                Dim dsBooking As DataSet = clsGet.Get_BookingOneRecord(Session("PayinBooking"))
                ViewState("dsBooking") = dsBooking
                ' dsConfig.Tables(0).Rows(0).Item("Head_Report_Compute")
                Dim HTML As String = ""
                HTML = Gen_Report(dsConfig.Tables(0).Rows(0).Item("Head_Report_Compute"), dsBooking.Tables(0).Rows(0).Item("MAgent_name"), dsBooking.Tables(0).Rows(0).Item("Vehicle_Text"), dsBooking.Tables(0).Rows(0).Item("Product_name"), dsBooking.Tables(0).Rows(0).Item("Vehicle_DWT"), dsBooking.Tables(0).Rows(0).Item("Vehicle_LOA"), dsBooking.Tables(0).Rows(0).Item("Product_Amount"), dsBooking.Tables(0).Rows(0).Item("Vehicle_Draft"), Gen_Body(Vat, dt), dsBooking.Tables(0).Rows(0).Item("Vehicle_GRT"))
                Literal1.Text = HTML

                btnConfirm.Visible = False
                Dim dtPayment As DataTable = clsGet.Get_Payment_Bank(Session("PayinBooking"), CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("Customer_no"))
                If (dtPayment.Rows.Count > 0) Then
                    If (dtPayment.Rows(0).Item("Status") & "" = "ED") Then
                        If Not (ddlBank.Items.FindByValue(dtPayment.Rows(0).Item("Bank") & "") Is Nothing) Then
                            ddlBank.SelectedValue = dtPayment.Rows(0).Item("Bank") & ""
                        End If
                        If (ddlBank.SelectedValue & "" = "KBANK") Then
                            With ddlAccount
                                .Items.Clear()
                                .ClearSelection()
                                .DataTextField = "Account"
                                .DataValueField = "Account"
                                Dim customer As Integer = 0
                                Try
                                    customer = CInt(CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("Customer_no"))
                                Catch ex As Exception
                                End Try
                                Dim dx As DataTable = GetBookingAccount(customer)
                                .DataSource = dx
                                .DataBind()
                                If dt.Rows.Count > 0 Then
                                    btnConfirm.Visible = True
                                End If
                            End With
                        Else
                            ddlAccount.Items.Clear()
                            btnConfirm.Visible = False
                        End If
                        If Not (ddlAccount.Items.FindByValue(dtPayment.Rows(0).Item("BankAccount") & "") Is Nothing) Then
                            ddlAccount.SelectedValue = dtPayment.Rows(0).Item("BankAccount") & ""
                        End If
                        Payment_MOney(dtPayment.Rows(0).Item("tracking") & "", "PAYMENT_INQUIRY")
                    End If
                End If
            End If

        End If
    End Sub

    Private Sub btnConfirm_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnConfirm.Click
        Payment_MOney("", "PAYMENT_REQUEST")
    End Sub

    Private Sub Payment_MOney(ByVal tracking As String, ByVal Request As String)
        If (ddlBank.SelectedValue = "0") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการเลือกธนาคารที่ต้องการทำการชำระเงิน');", True)
            Exit Sub
        ElseIf (ddlBank.SelectedValue = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('ธนาคารนี้ยังไม่เปิดให้ใช้งาน');", True)
            Exit Sub
        End If

        '**************************************************************************************
        'ทำการ Connect กับธนาคารเพื่อทำการจ่ายเงินจะได้เลขที่บัญชีมาด้วย
        '**************************************************************************************

        If (ViewState("Amt") Is Nothing) Then
            Exit Sub
        End If
        If (ddlAccount.SelectedValue & "" = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('ไม่พบข้อมูลรายการบัญชี');", True)
            Exit Sub
        End If
        Dim clsDB As New ClassDatabase()
        Dim conn As New System.Data.SqlClient.SqlConnection(connString)
        If (tracking = "") Then
            tracking = clsDB.GeneratorID(conn, "Payment_Bank")
        End If
        Dim Customer As Integer = 0
        Try
            Customer = CInt(CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("Customer_no"))
        Catch ex As Exception

        End Try
        Dim Status As String = ""
        Dim Detail As String = ""
        If Not (PaymentBank(tracking, Customer, ViewState("Amt").ToString.Replace(",", ""), ddlAccount.SelectedValue, Status, Detail, Request)) Then
            If Not (InsertPayment_Bank(tracking, Session("PayinBooking"), CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("Customer_no"), ViewState("Amt"), ddlAccount.SelectedValue, Date.Now.ToString("dd MMM yyyy HH:mm:ss", New CultureInfo("en-US")), Status, Detail, ddlBank.SelectedValue)) Then
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('เกิดความผิดพลาดในการบันทึกข้อมูล');", True)
            End If
            If (Status = "ED") Then
                btnConfirm.Visible = False
                ddlAccount.Enabled = False
                ddlBank.Enabled = False
                rowAlert.Visible = True
            End If
            Exit Sub
        Else
            If (Status = "ED") Then
                btnConfirm.Visible = False
                ddlAccount.Enabled = False
                ddlBank.Enabled = False
                rowAlert.Visible = True
            End If
            If Not (InsertPayment_Bank(tracking, Session("PayinBooking"), CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("Customer_no"), ViewState("Amt"), ddlAccount.SelectedValue, Date.Now.ToString("dd MMM yyyy HH:mm:ss", New CultureInfo("en-US")), Status, Detail, ddlBank.SelectedValue)) Then
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('เกิดความผิดพลาดในการบันทึกข้อมูล');", True)
                Exit Sub
            End If
        End If


        If (UpdateStatusPayin()) Then

            '**************************************************************************************
            'Advance Payment
            '**************************************************************************************
            Dim Bill_Date As String = Date.Now.ToString("yyyyMMdd", New CultureInfo("en-US"))
            Dim Bank As String = "TR " & ddlBank.SelectedValue
            Dim ClassSAP As New Class_SAP
            Dim SAP_Data As Object = ClassSAP.SAP_OPEN()
            Dim CheckCreditLimit As Boolean = True
            Dim dt As DataTable = ViewState("dt")
            Dim Iport_Number As String = clsGet.Get_IportNumber()
            Iport_Number = Iport_Number.Substring(0, 8) & "-" & (CInt(Iport_Number.Substring(9, 3)) - 1).ToString("000")
            Dim dsBooking As DataSet = ViewState("dsBooking")
            Dim BillTo As String = ""
            If (dsBooking.Tables(0).Rows(0).Item("Bill_To_no") & "" <> "") Then
                BillTo = dsBooking.Tables(0).Rows(0).Item("Bill_To_no") & ""
            End If
            Dim Plant As String = "1104"
            If (dsBooking.Tables(0).Rows(0).Item("PORT") & "" = "L") Then
                Plant = "1103"
            End If
            '***************************************
            'Parameter For Division 11

            Dim Customer11 As SAP_IPORT.ZBAPI_CUSTOMER
            Dim CustomerTable11_0 As New SAP_IPORT.ZBAPI_CUSTOMERTable
            Dim CustomerTable11_1 As New SAP_IPORT.ZBAPI_CUSTOMERTable
            Dim CustomerTable11_5 As New SAP_IPORT.ZBAPI_CUSTOMERTable
            Dim SumAmount11_0 As Double = "0"
            Dim WHTAmount11_0 As Double = "0.00"
            Dim SumAmount11_1 As Double = "0"
            Dim WHTAmount11_1 As Double = "0.00"
            Dim SumAmount11_5 As Double = "0"
            Dim WHTAmount11_5 As Double = "0.00"
            Dim ZBABI_VT11 As New SAP_IPORT.ZBAPI_VAT
            Dim ZBABI_BK11 As New SAP_IPORT.ZBAPI_BANK
            Dim ZBABI_HD11 As New SAP_IPORT.ZBAPI_HEADER
            Dim BABITable11 As New SAP_IPORT.BAPIRET1Table
            Dim Ex_Bukrs11 As String = ""
            Dim Ex_Gjahr11 As String = ""
            Dim Ex_Belnr11 As String = ""

            Dim BABIHEADER11 As New SAP_IPORT.ZBAPI_SALES_HEADER
            Dim EX_SALES_NO11 As String = ""
            Dim EX_BILL_NO11 As String = ""
            Dim EX_BUKRS11_Cash As String = ""
            Dim EX_BELNR11_Cash As String = ""
            Dim EX_GJAHR11_Cash As String = ""
            Dim BABIONETIME11 As New SAP_IPORT.ZBAPI_SALES_ONETIME
            Dim BABI_SALE11 As SAP_IPORT.ZBAPI_SALES_ITEMS
            Dim SALE_TABLE11_0 As New SAP_IPORT.ZBAPI_SALES_ITEMSTable
            Dim SALE_TABLE11_1 As New SAP_IPORT.ZBAPI_SALES_ITEMSTable
            Dim SALE_TABLE11_5 As New SAP_IPORT.ZBAPI_SALES_ITEMSTable
            Dim BABITable11_Cash As New SAP_IPORT.BAPIRET1Table

            '******************************************

            '***************************************
            'Parameter For Division 12
            Dim Customer12 As SAP_IPORT.ZBAPI_CUSTOMER
            Dim CustomerTable12_0 As New SAP_IPORT.ZBAPI_CUSTOMERTable
            Dim CustomerTable12_1 As New SAP_IPORT.ZBAPI_CUSTOMERTable
            Dim CustomerTable12_5 As New SAP_IPORT.ZBAPI_CUSTOMERTable
            Dim SumAmount12_0 As Double = "0"
            Dim WHTAmount12_0 As Double = "0.00"
            Dim SumAmount12_1 As Double = "0"
            Dim WHTAmount12_1 As Double = "0.00"
            Dim SumAmount12_5 As Double = "0"
            Dim WHTAmount12_5 As Double = "0.00"
            Dim ZBABI_VT12 As New SAP_IPORT.ZBAPI_VAT
            Dim ZBABI_BK12 As New SAP_IPORT.ZBAPI_BANK
            Dim ZBABI_HD12 As New SAP_IPORT.ZBAPI_HEADER
            Dim BABITable12 As New SAP_IPORT.BAPIRET1Table
            Dim Ex_Bukrs12 As String = ""
            Dim Ex_Gjahr12 As String = ""
            Dim Ex_Belnr12 As String = ""

            Dim BABIHEADER12 As New SAP_IPORT.ZBAPI_SALES_HEADER
            Dim EX_SALES_NO12 As String = ""
            Dim EX_BILL_NO12 As String = ""
            Dim EX_BUKRS12_Cash As String = ""
            Dim EX_BELNR12_Cash As String = ""
            Dim EX_GJAHR12_Cash As String = ""
            Dim BABIONETIME12 As New SAP_IPORT.ZBAPI_SALES_ONETIME
            Dim BABI_SALE12 As SAP_IPORT.ZBAPI_SALES_ITEMS
            Dim SALE_TABLE12_0 As New SAP_IPORT.ZBAPI_SALES_ITEMSTable
            Dim SALE_TABLE12_1 As New SAP_IPORT.ZBAPI_SALES_ITEMSTable
            Dim SALE_TABLE12_5 As New SAP_IPORT.ZBAPI_SALES_ITEMSTable
            Dim BABITable12_Cash As New SAP_IPORT.BAPIRET1Table


            '******************************************
            For Each dr As DataRow In dt.Rows
                'ทำการแยกเฉพาะ Division เท่านั้นเพราะลูกค้าได้ทำการแยกตามคนที่เข้ามาแล้ว
                If (dr.Item("Division") = "11") Then

                    Dim Target_QU As String = dr.Item("BASE_UOM")
                    Dim Target_Qty As String = ""
                    Dim Price As String = ""
                    If (Target_QU = "JOB") Then
                        Target_Qty = "1"
                        Price = dr.Item("data4")
                    Else
                        Target_Qty = (CDbl(dr.Item("data4")) / CDbl(dr.Item("data2"))).ToString("############0.00")
                        Price = dr.Item("data2")
                    End If
                    If (dr.Item("TAXCLASS") = "0") Then
                        SumAmount11_0 = SumAmount11_0 + CDbl(dr.Item("data4"))
                        If (dr.Item("MAT_WHT") & "" = "01") Or (dr.Item("MAT_WHT") & "" = "02") Or (dr.Item("MAT_WHT") & "" = "03") Then
                            '1%
                            WHTAmount11_0 = (WHTAmount11_0 + CDbl(((CDbl(dr.Item("data4")) * 1) / 100).ToString("#########0.00"))).ToString("###############0.00")
                        ElseIf (dr.Item("MAT_WHT") & "" = "04") Or (dr.Item("MAT_WHT") & "" = "05") Then
                            '3%
                            WHTAmount11_0 = (WHTAmount11_0 + CDbl(((CDbl(dr.Item("data4")) * 3) / 100).ToString("#########0.00"))).ToString("###############0.00")
                        ElseIf (dr.Item("MAT_WHT") & "" = "06") Or (dr.Item("MAT_WHT") & "" = "22") Then
                            '5%
                            WHTAmount11_0 = (WHTAmount11_0 + CDbl(((CDbl(dr.Item("data4")) * 5) / 100).ToString("#########0.00"))).ToString("###############0.00")
                        End If


                        Customer11 = New SAP_IPORT.ZBAPI_CUSTOMER
                        Customer11.Customer = CInt(CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("Customer_no"))
                        Customer11.Amount = dr.Item("data4")
                        Customer11.Item_Text = dr.Item("MAT_NAME")
                        Customer11.Material = CInt(dr.Item("Material")).ToString()
                        Customer11.Price = Price
                        Customer11.Quantity = Target_Qty
                        Customer11.Unit = Target_QU
                        Customer11.Wht_Code = dr.Item("MAT_WHT")

                        CustomerTable11_0.Insert(0, Customer11)

                        BABI_SALE11 = New SAP_IPORT.ZBAPI_SALES_ITEMS
                        BABI_SALE11.Material = dr.Item("Material")
                        BABI_SALE11.Plant = Plant
                        BABI_SALE11.Target_Qty = Target_Qty
                        BABI_SALE11.Target_Qu = Target_QU
                        BABI_SALE11.Cond_Type = ""
                        BABI_SALE11.Discount = "0"
                        Try
                            Dim dtLowRate As DataTable = clsGet.Get_ROWRATE_MAT(dr.Item("Material"))
                            Dim LowRate As Double = "0"
                            Dim chkROw As Boolean = False
                            For Each dL As DataRow In dtLowRate.Rows
                                If (CDbl(dr.Item("data4")) = CDbl(dL.Item("LowRate"))) Then
                                    LowRate = dr.Item("data4")
                                    chkROw = True
                                    Exit For
                                End If
                            Next
                            If (chkROw) Then
                                BABI_SALE11.Amount = LowRate
                                BABI_SALE11.Condvalue = LowRate
                            Else
                                BABI_SALE11.Amount = Price
                                BABI_SALE11.Condvalue = "0"
                            End If
                        Catch ex As Exception
                            BABI_SALE11.Amount = Price
                            BABI_SALE11.Condvalue = "0"
                        End Try

                        SALE_TABLE11_0.Insert(0, BABI_SALE11)
                    ElseIf (dr.Item("TAXCLASS") = "1") Then
                        SumAmount11_1 = SumAmount11_1 + CDbl(dr.Item("data4"))
                        If (dr.Item("MAT_WHT") & "" = "01") Or (dr.Item("MAT_WHT") & "" = "02") Or (dr.Item("MAT_WHT") & "" = "03") Then
                            '1%
                            WHTAmount11_1 = (WHTAmount11_1 + CDbl(((CDbl(dr.Item("data4")) * 1) / 100).ToString("#########0.00"))).ToString("###############0.00")
                        ElseIf (dr.Item("MAT_WHT") & "" = "04") Or (dr.Item("MAT_WHT") & "" = "05") Then
                            '3%
                            WHTAmount11_1 = (WHTAmount11_1 + CDbl(((CDbl(dr.Item("data4")) * 3) / 100).ToString("#########0.00"))).ToString("###############0.00")
                        ElseIf (dr.Item("MAT_WHT") & "" = "06") Or (dr.Item("MAT_WHT") & "" = "22") Then
                            '5%
                            WHTAmount11_1 = (WHTAmount11_1 + CDbl(((CDbl(dr.Item("data4")) * 5) / 100).ToString("#########0.00"))).ToString("###############0.00")
                        End If


                        Customer11 = New SAP_IPORT.ZBAPI_CUSTOMER
                        Customer11.Customer = CInt(CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("Customer_no"))
                        Customer11.Amount = dr.Item("data4")
                        Customer11.Item_Text = dr.Item("MAT_NAME")
                        Customer11.Material = CInt(dr.Item("Material")).ToString()
                        Customer11.Price = Price
                        Customer11.Quantity = Target_Qty
                        Customer11.Unit = Target_QU
                        Customer11.Wht_Code = dr.Item("MAT_WHT")

                        CustomerTable11_1.Insert(0, Customer11)

                        BABI_SALE11 = New SAP_IPORT.ZBAPI_SALES_ITEMS
                        BABI_SALE11.Material = dr.Item("Material")
                        BABI_SALE11.Plant = Plant
                        BABI_SALE11.Target_Qty = Target_Qty
                        BABI_SALE11.Target_Qu = Target_QU
                        BABI_SALE11.Cond_Type = ""
                        BABI_SALE11.Discount = "0"
                        Try
                            Dim dtLowRate As DataTable = clsGet.Get_ROWRATE_MAT(dr.Item("Material"))
                            Dim LowRate As Double = "0"
                            Dim chkROw As Boolean = False
                            For Each dL As DataRow In dtLowRate.Rows
                                If (CDbl(dr.Item("data4")) = CDbl(dL.Item("LowRate"))) Then
                                    LowRate = dr.Item("data4")
                                    chkROw = True
                                    Exit For
                                End If
                            Next
                            If (chkROw) Then
                                BABI_SALE11.Amount = LowRate
                                BABI_SALE11.Condvalue = LowRate
                            Else
                                BABI_SALE11.Amount = Price
                                BABI_SALE11.Condvalue = "0"
                            End If
                        Catch ex As Exception
                            BABI_SALE11.Amount = Price
                            BABI_SALE11.Condvalue = "0"
                        End Try

                        SALE_TABLE11_1.Insert(0, BABI_SALE11)
                    ElseIf (dr.Item("TAXCLASS") = "5") Then
                        SumAmount11_5 = SumAmount11_5 + CDbl(dr.Item("data4"))
                        If (dr.Item("MAT_WHT") & "" = "01") Or (dr.Item("MAT_WHT") & "" = "02") Or (dr.Item("MAT_WHT") & "" = "03") Then
                            '1%
                            WHTAmount11_5 = (WHTAmount11_5 + CDbl(((CDbl(dr.Item("data4")) * 1) / 100).ToString("#########0.00"))).ToString("###############0.00")
                        ElseIf (dr.Item("MAT_WHT") & "" = "04") Or (dr.Item("MAT_WHT") & "" = "05") Then
                            '3%
                            WHTAmount11_5 = (WHTAmount11_5 + CDbl(((CDbl(dr.Item("data4")) * 3) / 100).ToString("#########0.00"))).ToString("###############0.00")
                        ElseIf (dr.Item("MAT_WHT") & "" = "06") Or (dr.Item("MAT_WHT") & "" = "22") Then
                            '5%
                            WHTAmount11_5 = (WHTAmount11_5 + CDbl(((CDbl(dr.Item("data4")) * 5) / 100).ToString("#########0.00"))).ToString("###############0.00")
                        End If


                        Customer11 = New SAP_IPORT.ZBAPI_CUSTOMER
                        Customer11.Customer = CInt(CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("Customer_no"))
                        Customer11.Amount = dr.Item("data4")
                        Customer11.Item_Text = dr.Item("MAT_NAME")
                        Customer11.Material = CInt(dr.Item("Material")).ToString()
                        Customer11.Price = Price
                        Customer11.Quantity = Target_Qty
                        Customer11.Unit = Target_QU
                        Customer11.Wht_Code = dr.Item("MAT_WHT")

                        CustomerTable11_5.Insert(0, Customer11)

                        BABI_SALE11 = New SAP_IPORT.ZBAPI_SALES_ITEMS
                        BABI_SALE11.Material = dr.Item("Material")
                        BABI_SALE11.Plant = Plant
                        BABI_SALE11.Target_Qty = Target_Qty
                        BABI_SALE11.Target_Qu = Target_QU
                        BABI_SALE11.Cond_Type = ""
                        BABI_SALE11.Discount = "0"
                        Try
                            Dim dtLowRate As DataTable = clsGet.Get_ROWRATE_MAT(dr.Item("Material"))
                            Dim LowRate As Double = "0"
                            Dim chkROw As Boolean = False
                            For Each dL As DataRow In dtLowRate.Rows
                                If (CDbl(dr.Item("data4")) = CDbl(dL.Item("LowRate"))) Then
                                    LowRate = dr.Item("data4")
                                    chkROw = True
                                    Exit For
                                End If
                            Next
                            If (chkROw) Then
                                BABI_SALE11.Amount = LowRate
                                BABI_SALE11.Condvalue = LowRate
                            Else
                                BABI_SALE11.Amount = Price
                                BABI_SALE11.Condvalue = "0"
                            End If
                        Catch ex As Exception
                            BABI_SALE11.Amount = Price
                            BABI_SALE11.Condvalue = "0"
                        End Try

                        SALE_TABLE11_5.Insert(0, BABI_SALE11)
                    End If


                Else

                    Dim Target_QU As String = dr.Item("BASE_UOM")
                    Dim Target_Qty As String = ""
                    Dim Price As String = ""
                    If (Target_QU = "JOB") Then
                        Target_Qty = "1"
                        Price = dr.Item("data4")
                    Else
                        Target_Qty = (CDbl(dr.Item("data4")) / CDbl(dr.Item("data2"))).ToString("############0.00")
                        Price = dr.Item("data2")
                    End If
                    If (dr.Item("TAXCLASS") = "0") Then
                        SumAmount12_0 = SumAmount12_0 + CDbl(dr.Item("data4"))
                        If (dr.Item("MAT_WHT") & "" = "01") Or (dr.Item("MAT_WHT") & "" = "02") Or (dr.Item("MAT_WHT") & "" = "03") Then
                            '1%
                            WHTAmount12_0 = (WHTAmount12_0 + CDbl(((CDbl(dr.Item("data4")) * 1) / 100).ToString("#########0.00"))).ToString("###############0.00")
                        ElseIf (dr.Item("MAT_WHT") & "" = "04") Or (dr.Item("MAT_WHT") & "" = "05") Then
                            '3%
                            WHTAmount12_0 = (WHTAmount12_0 + CDbl(((CDbl(dr.Item("data4")) * 3) / 100).ToString("#########0.00"))).ToString("###############0.00")
                        ElseIf (dr.Item("MAT_WHT") & "" = "06") Or (dr.Item("MAT_WHT") & "" = "22") Then
                            '5%
                            WHTAmount12_0 = (WHTAmount12_0 + CDbl(((CDbl(dr.Item("data4")) * 5) / 100).ToString("#########0.00"))).ToString("###############0.00")
                        End If


                        Customer12 = New SAP_IPORT.ZBAPI_CUSTOMER
                        Customer12.Customer = CInt(CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("Customer_no"))
                        Customer12.Amount = dr.Item("data4")
                        Customer12.Item_Text = dr.Item("MAT_NAME")
                        Customer12.Material = CInt(dr.Item("Material")).ToString()
                        Customer12.Price = Price
                        Customer12.Quantity = Target_Qty
                        Customer12.Unit = Target_QU
                        Customer12.Wht_Code = dr.Item("MAT_WHT")

                        CustomerTable12_0.Insert(0, Customer12)

                        BABI_SALE12 = New SAP_IPORT.ZBAPI_SALES_ITEMS
                        BABI_SALE12.Material = dr.Item("Material")
                        BABI_SALE12.Plant = Plant
                        BABI_SALE12.Target_Qty = Target_Qty
                        BABI_SALE12.Target_Qu = Target_QU
                        BABI_SALE12.Cond_Type = ""
                        BABI_SALE12.Discount = "0"
                        Try
                            Dim dtLowRate As DataTable = clsGet.Get_ROWRATE_MAT(dr.Item("Material"))
                            Dim LowRate As Double = "0"
                            Dim chkROw As Boolean = False
                            For Each dL As DataRow In dtLowRate.Rows
                                If (CDbl(dr.Item("data4")) = CDbl(dL.Item("LowRate"))) Then
                                    LowRate = dr.Item("data4")
                                    chkROw = True
                                    Exit For
                                End If
                            Next
                            If (chkROw) Then
                                BABI_SALE12.Amount = LowRate
                                BABI_SALE12.Condvalue = LowRate
                            Else
                                BABI_SALE12.Amount = Price
                                BABI_SALE12.Condvalue = "0"
                            End If
                        Catch ex As Exception
                            BABI_SALE12.Amount = Price
                            BABI_SALE12.Condvalue = "0"
                        End Try

                        SALE_TABLE12_0.Insert(0, BABI_SALE12)
                    ElseIf (dr.Item("TAXCLASS") = "1") Then
                        SumAmount12_1 = SumAmount12_1 + CDbl(dr.Item("data4"))
                        If (dr.Item("MAT_WHT") & "" = "01") Or (dr.Item("MAT_WHT") & "" = "02") Or (dr.Item("MAT_WHT") & "" = "03") Then
                            '1%
                            WHTAmount12_1 = (WHTAmount12_1 + CDbl(((CDbl(dr.Item("data4")) * 1) / 100).ToString("#########0.00"))).ToString("###############0.00")
                        ElseIf (dr.Item("MAT_WHT") & "" = "04") Or (dr.Item("MAT_WHT") & "" = "05") Then
                            '3%
                            WHTAmount12_1 = (WHTAmount12_1 + CDbl(((CDbl(dr.Item("data4")) * 3) / 100).ToString("#########0.00"))).ToString("###############0.00")
                        ElseIf (dr.Item("MAT_WHT") & "" = "06") Or (dr.Item("MAT_WHT") & "" = "22") Then
                            '5%
                            WHTAmount12_1 = (WHTAmount12_1 + CDbl(((CDbl(dr.Item("data4")) * 5) / 100).ToString("#########0.00"))).ToString("###############0.00")
                        End If


                        Customer12 = New SAP_IPORT.ZBAPI_CUSTOMER
                        Customer12.Customer = CInt(CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("Customer_no"))
                        Customer12.Amount = dr.Item("data4")
                        Customer12.Item_Text = dr.Item("MAT_NAME")
                        Customer12.Material = CInt(dr.Item("Material")).ToString()
                        Customer12.Price = Price
                        Customer12.Quantity = Target_Qty
                        Customer12.Unit = Target_QU
                        Customer12.Wht_Code = dr.Item("MAT_WHT")

                        CustomerTable12_1.Insert(0, Customer12)

                        BABI_SALE12 = New SAP_IPORT.ZBAPI_SALES_ITEMS
                        BABI_SALE12.Material = dr.Item("Material")
                        BABI_SALE12.Plant = Plant
                        BABI_SALE12.Target_Qty = Target_Qty
                        BABI_SALE12.Target_Qu = Target_QU
                        BABI_SALE12.Cond_Type = ""
                        BABI_SALE12.Discount = "0"
                        Try
                            Dim dtLowRate As DataTable = clsGet.Get_ROWRATE_MAT(dr.Item("Material"))
                            Dim LowRate As Double = "0"
                            Dim chkROw As Boolean = False
                            For Each dL As DataRow In dtLowRate.Rows
                                If (CDbl(dr.Item("data4")) = CDbl(dL.Item("LowRate"))) Then
                                    LowRate = dr.Item("data4")
                                    chkROw = True
                                    Exit For
                                End If
                            Next
                            If (chkROw) Then
                                BABI_SALE12.Amount = LowRate
                                BABI_SALE12.Condvalue = LowRate
                            Else
                                BABI_SALE12.Amount = Price
                                BABI_SALE12.Condvalue = "0"
                            End If
                        Catch ex As Exception
                            BABI_SALE12.Amount = Price
                            BABI_SALE12.Condvalue = "0"
                        End Try

                        SALE_TABLE12_1.Insert(0, BABI_SALE12)
                    ElseIf (dr.Item("TAXCLASS") = "5") Then
                        SumAmount12_5 = SumAmount12_5 + CDbl(dr.Item("data4"))
                        If (dr.Item("MAT_WHT") & "" = "01") Or (dr.Item("MAT_WHT") & "" = "02") Or (dr.Item("MAT_WHT") & "" = "03") Then
                            '1%
                            WHTAmount12_5 = (WHTAmount12_5 + CDbl(((CDbl(dr.Item("data4")) * 1) / 100).ToString("#########0.00"))).ToString("###############0.00")
                        ElseIf (dr.Item("MAT_WHT") & "" = "04") Or (dr.Item("MAT_WHT") & "" = "05") Then
                            '3%
                            WHTAmount12_5 = (WHTAmount12_5 + CDbl(((CDbl(dr.Item("data4")) * 3) / 100).ToString("#########0.00"))).ToString("###############0.00")
                        ElseIf (dr.Item("MAT_WHT") & "" = "06") Or (dr.Item("MAT_WHT") & "" = "22") Then
                            '5%
                            WHTAmount12_5 = (WHTAmount12_5 + CDbl(((CDbl(dr.Item("data4")) * 5) / 100).ToString("#########0.00"))).ToString("###############0.00")
                        End If


                        Customer12 = New SAP_IPORT.ZBAPI_CUSTOMER
                        Customer12.Customer = CInt(CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("Customer_no"))
                        Customer12.Amount = dr.Item("data4")
                        Customer12.Item_Text = dr.Item("MAT_NAME")
                        Customer12.Material = CInt(dr.Item("Material")).ToString()
                        Customer12.Price = Price
                        Customer12.Quantity = Target_Qty
                        Customer12.Unit = Target_QU
                        Customer12.Wht_Code = dr.Item("MAT_WHT")

                        CustomerTable12_5.Insert(0, Customer12)

                        BABI_SALE12 = New SAP_IPORT.ZBAPI_SALES_ITEMS
                        BABI_SALE12.Material = dr.Item("Material")
                        BABI_SALE12.Plant = Plant
                        BABI_SALE12.Target_Qty = Target_Qty
                        BABI_SALE12.Target_Qu = Target_QU
                        BABI_SALE12.Cond_Type = ""
                        BABI_SALE12.Discount = "0"
                        Try
                            Dim dtLowRate As DataTable = clsGet.Get_ROWRATE_MAT(dr.Item("Material"))
                            Dim LowRate As Double = "0"
                            Dim chkROw As Boolean = False
                            For Each dL As DataRow In dtLowRate.Rows
                                If (CDbl(dr.Item("data4")) = CDbl(dL.Item("LowRate"))) Then
                                    LowRate = dr.Item("data4")
                                    chkROw = True
                                    Exit For
                                End If
                            Next
                            If (chkROw) Then
                                BABI_SALE12.Amount = LowRate
                                BABI_SALE12.Condvalue = LowRate
                            Else
                                BABI_SALE12.Amount = Price
                                BABI_SALE12.Condvalue = "0"
                            End If
                        Catch ex As Exception
                            BABI_SALE12.Amount = Price
                            BABI_SALE12.Condvalue = "0"
                        End Try

                        SALE_TABLE12_5.Insert(0, BABI_SALE12)

                    End If

                End If

            Next

            Dim condition As Boolean = True
            Dim tran As System.Data.SqlClient.SqlTransaction = Nothing
            Application.Lock()
            conn.Open()
            tran = clsDB.BeginTran(conn)

            If (CustomerTable11_0.Count > 0) Then
                'มี Mat ที่ใช้ Division 11
                Iport_Number = Iport_Number.Substring(0, 8) & "-" & (CInt(Iport_Number.Substring(9, 3)) + 1).ToString("000")
                ZBABI_VT11.Amount = SumAmount11_0.ToString("############.00")
                ZBABI_VT11.Vat = ((SumAmount11_0 * CDbl(ViewState("VAT"))) / 100).ToString("############.00")

                '  ZBABI_BK11.Amount = (CDbl(SumAmount11_0 + CDbl(((SumAmount11_0 * CDbl(ViewState("VAT"))) / 100).ToString("############.00"))) - WHTAmount11_0).ToString("############.00")

                ZBABI_BK11.Amount = (CDbl(SumAmount11_0 + CDbl(((SumAmount11_0 * CDbl(ViewState("VAT"))) / 100).ToString("############.00")))).ToString("############.00")
                ZBABI_BK11.Gl_Account = "10302031"  'เลขบัญชีต้องดึงจาก Web service Bank
                ZBABI_BK11.Item_Text = "TR " & ddlBank.SelectedValue
                ZBABI_BK11.Value_Date = Date.Now.ToString("yyyyMMdd", New CultureInfo("en-US")) '"20100921"

                ZBABI_HD11.Bupla = "N014"
                ZBABI_HD11.Doc_Date = Date.Now.ToString("yyyyMMdd", New CultureInfo("en-US")) '"20100730"
                ZBABI_HD11.Header_Txt = "R|" & dsBooking.Tables(0).Rows(0).Item("Vehicle_Text") '"R|LEO GLORY"
                ZBABI_HD11.Pstng_Date = Date.Now.ToString("yyyyMMdd", New CultureInfo("en-US")) '"20100921"

                SAP_Data.Zbapi_Iport_Advanced_Payment(ZBABI_BK11, ZBABI_HD11, Iport_Number, BillTo, ZBABI_VT11, Ex_Belnr11, Ex_Bukrs11, Ex_Gjahr11, CustomerTable11_1, BABITable11)

                If (Ex_Belnr11 <> "") And (Ex_Bukrs11 <> "") And (Ex_Gjahr11 <> "") Then
                    'Advanced Payment ผ่าน
                    Dim Bill_To As String = ""
                    Dim sold_to As String = ""
                    If (CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("Customer_no") = dsBooking.Tables(0).Rows(0).Item("MAgent_no")) Then
                        Bill_To = dsBooking.Tables(0).Rows(0).Item("MAgent_no")
                        sold_to = Bill_To
                        If (dsBooking.Tables(0).Rows(0).Item("Bill_To_no") & "" <> "") Then
                            Bill_To = dsBooking.Tables(0).Rows(0).Item("Bill_To_no") & ""
                        End If
                    Else
                        Bill_To = CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("Customer_no")
                        sold_to = Bill_To
                    End If

                    BABIHEADER11.Doc_Type = "ZSRC"
                    BABIHEADER11.Req_Date_H = Date.Now.ToString("yyyyMMdd", New CultureInfo("en-US"))
                    BABIHEADER11.Sold_To = sold_to
                    BABIHEADER11.Bill_To = Bill_To
                    BABIHEADER11.Ship_Name = dsBooking.Tables(0).Rows(0).Item("Vehicle_Text")
                    BABIHEADER11.Division = "11"

                    SAP_Data.Zbapi_Iport_Sd_Proc_Cash_Sales(Bill_Date, "", Iport_Number, BABIONETIME11, BABIHEADER11, EX_BELNR11_Cash, EX_BILL_NO11, EX_BUKRS11_Cash, EX_GJAHR11_Cash, EX_SALES_NO11, BABITable11_Cash, SALE_TABLE11_0)

                    If (EX_SALES_NO11 <> "") Then
                        'Insert Advanced Payment

                        If (clsFc.InsertIportNumber(Iport_Number, conn, tran) > 0) Then
                            For Each dr As SAP_IPORT.BAPIRET1 In BABITable11
                                If (clsFc.InsertSAP_Table_MESSAGE(Iport_Number, "Zbapi_Iport_Advanced_Payment", dr.Type, dr.Id, dr.Number, dr.Message, dr.Log_No, dr.Log_Msg_No, dr.Message_V1, dr.Message_V2, dr.Message_V3, dr.Message_V4, conn, tran) < 1) Then
                                    condition = False
                                End If
                            Next
                            If (condition) Then
                                If (clsFc.InsertSAP_Advanced_Payment(Session("PayinBooking"), Iport_Number, ZBABI_BK11.Gl_Account, ZBABI_BK11.Item_Text, ZBABI_BK11.Value_Date, ZBABI_HD11.Bupla, ZBABI_HD11.Doc_Date, ZBABI_HD11.Header_Txt, ZBABI_HD11.Pstng_Date, ZBABI_VT11.Amount, ZBABI_VT11.Vat, Ex_Bukrs11, Ex_Gjahr11, Ex_Belnr11, WHTAmount11_0, "E", conn, tran) > 0) Then
                                    For Each dr As SAP_IPORT.ZBAPI_CUSTOMER In CustomerTable11_0
                                        If (dr.Material <> "") Then
                                            If (clsFc.InsertSAP_Advanced_Payment_Material(Iport_Number, dr.Customer, dr.Amount, dr.Item_Text, dr.Material, dr.Price, dr.Quantity, dr.Unit, dr.Wht_Code, conn, tran) < 1) Then
                                                condition = False
                                            End If
                                        End If
                                    Next
                                Else
                                    condition = False
                                End If
                            End If
                        Else
                            condition = False
                        End If

                        'Insert Cash Sale
                        Dim chkcredit As Boolean = True   ' True คือผ่าน
                        For Each dr As SAP_IPORT.BAPIRET1 In BABITable11_Cash
                            If (dr.Type = "E") And (dr.Id = "VF") And (dr.Number = "140") Then
                                chkcredit = False
                                CheckCreditLimit = False
                            End If
                            If (clsFc.InsertSAP_Table_MESSAGE(Iport_Number, "Zbapi_Iport_Sd_Proc_Cash_Sales", dr.Type, dr.Id, dr.Number, dr.Message, dr.Log_No, dr.Log_Msg_No, dr.Message_V1, dr.Message_V2, dr.Message_V3, dr.Message_V4, conn, tran) < 1) Then
                                condition = False
                            End If
                        Next
                        If (condition) Then
                            Dim Credit_limit As String = ""
                            If (chkcredit) Then
                                Credit_limit = "Y"
                            Else
                                Credit_limit = "N"
                            End If
                            If (clsFc.InsertSaleOrder(Session("PayinBooking"), Iport_Number, BABIHEADER11.Doc_Type, BABIHEADER11.Req_Date_H, BABIHEADER11.Sold_To, BABIHEADER11.Bill_To, BABIHEADER11.Ship_Name, BABIHEADER11.Division, EX_SALES_NO11, EX_BILL_NO11, Credit_limit, EX_BUKRS11_Cash, EX_BELNR11_Cash, EX_GJAHR11_Cash, Bill_Date, conn, tran) > 0) Then
                                For Each dr As SAP_IPORT.ZBAPI_SALES_ITEMS In SALE_TABLE11_0
                                    If (dr.Material <> "") Then
                                        If (clsFc.InsertSAP_Sale_Order_Material(Iport_Number, dr.Material, dr.Plant, dr.Target_Qty, dr.Target_Qu, dr.Cond_Type, dr.Discount, dr.Amount, dr.Condvalue, conn, tran) < 1) Then
                                            condition = False
                                        End If
                                    End If
                                Next

                            Else
                                condition = False
                            End If
                        End If
                    Else
                        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('Can not be Create Sale Order ');", True)
                    End If
                Else
                    ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('Can not be Create Advanced Payment ');", True)
                End If
            End If
            If (CustomerTable11_1.Count > 0) Then
                'มี Mat ที่ใช้ Division 11
                Iport_Number = Iport_Number.Substring(0, 8) & "-" & (CInt(Iport_Number.Substring(9, 3)) + 1).ToString("000")
                ZBABI_VT11.Amount = SumAmount11_1.ToString("############.00")
                ZBABI_VT11.Vat = ((SumAmount11_1 * CDbl(ViewState("VAT"))) / 100).ToString("############.00")

                ' ZBABI_BK11.Amount = (CDbl(SumAmount11_1 + CDbl(((SumAmount11_1 * CDbl(ViewState("VAT"))) / 100).ToString("############.00"))) - WHTAmount11_1).ToString("############.00")

                ZBABI_BK11.Amount = (CDbl(SumAmount11_1 + CDbl(((SumAmount11_1 * CDbl(ViewState("VAT"))) / 100).ToString("############.00")))).ToString("############.00")
                ZBABI_BK11.Gl_Account = "10302031"  'เลขบัญชีต้องดึงจาก Web service Bank
                ZBABI_BK11.Item_Text = "TR " & ddlBank.SelectedValue
                ZBABI_BK11.Value_Date = Date.Now.ToString("yyyyMMdd", New CultureInfo("en-US")) '"20100921"

                ZBABI_HD11.Bupla = "N014"
                ZBABI_HD11.Doc_Date = Date.Now.ToString("yyyyMMdd", New CultureInfo("en-US")) '"20100730"
                ZBABI_HD11.Header_Txt = "R|" & dsBooking.Tables(0).Rows(0).Item("Vehicle_Text") '"R|LEO GLORY"
                ZBABI_HD11.Pstng_Date = Date.Now.ToString("yyyyMMdd", New CultureInfo("en-US")) '"20100921"

                SAP_Data.Zbapi_Iport_Advanced_Payment(ZBABI_BK11, ZBABI_HD11, Iport_Number, BillTo, ZBABI_VT11, Ex_Belnr11, Ex_Bukrs11, Ex_Gjahr11, CustomerTable11_1, BABITable11)

                If (Ex_Belnr11 <> "") And (Ex_Bukrs11 <> "") And (Ex_Gjahr11 <> "") Then
                    'Advanced Payment ผ่าน
                    Dim Bill_To As String = ""
                    Dim Sold_To As String = ""
                    If (CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("Customer_no") = dsBooking.Tables(0).Rows(0).Item("MAgent_no")) Then
                        Bill_To = dsBooking.Tables(0).Rows(0).Item("MAgent_no")
                        Sold_To = Bill_To
                        If (dsBooking.Tables(0).Rows(0).Item("Bill_To_no") & "" <> "") Then
                            Bill_To = dsBooking.Tables(0).Rows(0).Item("Bill_To_no") & ""
                        End If
                    Else
                        Bill_To = CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("Customer_no")
                        Sold_To = Bill_To
                    End If

                    BABIHEADER11.Doc_Type = "ZSRC"
                    BABIHEADER11.Req_Date_H = Date.Now.ToString("yyyyMMdd", New CultureInfo("en-US"))
                    BABIHEADER11.Sold_To = Sold_To
                    BABIHEADER11.Bill_To = Bill_To
                    BABIHEADER11.Ship_Name = dsBooking.Tables(0).Rows(0).Item("Vehicle_Text")
                    BABIHEADER11.Division = "11"

                    SAP_Data.Zbapi_Iport_Sd_Proc_Cash_Sales(Bill_Date, "", Iport_Number, BABIONETIME11, BABIHEADER11, EX_BELNR11_Cash, EX_BILL_NO11, EX_BUKRS11_Cash, EX_GJAHR11_Cash, EX_SALES_NO11, BABITable11_Cash, SALE_TABLE11_1)

                    If (EX_SALES_NO11 <> "") Then
                        'Insert Advanced Payment

                        If (clsFc.InsertIportNumber(Iport_Number, conn, tran) > 0) Then
                            For Each dr As SAP_IPORT.BAPIRET1 In BABITable11
                                If (clsFc.InsertSAP_Table_MESSAGE(Iport_Number, "Zbapi_Iport_Advanced_Payment", dr.Type, dr.Id, dr.Number, dr.Message, dr.Log_No, dr.Log_Msg_No, dr.Message_V1, dr.Message_V2, dr.Message_V3, dr.Message_V4, conn, tran) < 1) Then
                                    condition = False
                                End If
                            Next
                            If (condition) Then
                                If (clsFc.InsertSAP_Advanced_Payment(Session("PayinBooking"), Iport_Number, ZBABI_BK11.Gl_Account, ZBABI_BK11.Item_Text, ZBABI_BK11.Value_Date, ZBABI_HD11.Bupla, ZBABI_HD11.Doc_Date, ZBABI_HD11.Header_Txt, ZBABI_HD11.Pstng_Date, ZBABI_VT11.Amount, ZBABI_VT11.Vat, Ex_Bukrs11, Ex_Gjahr11, Ex_Belnr11, WHTAmount11_1, "E", conn, tran) > 0) Then
                                    For Each dr As SAP_IPORT.ZBAPI_CUSTOMER In CustomerTable11_1
                                        If (dr.Material <> "") Then
                                            If (clsFc.InsertSAP_Advanced_Payment_Material(Iport_Number, dr.Customer, dr.Amount, dr.Item_Text, dr.Material, dr.Price, dr.Quantity, dr.Unit, dr.Wht_Code, conn, tran) < 1) Then
                                                condition = False
                                            End If
                                        End If
                                    Next
                                Else
                                    condition = False
                                End If
                            End If
                        Else
                            condition = False
                        End If

                        'Insert Cash Sale
                        Dim chkcredit As Boolean = True   ' True คือผ่าน
                        For Each dr As SAP_IPORT.BAPIRET1 In BABITable11_Cash
                            If (dr.Type = "E") And (dr.Id = "VF") And (dr.Number = "140") Then
                                chkcredit = False
                                CheckCreditLimit = False
                            End If
                            If (clsFc.InsertSAP_Table_MESSAGE(Iport_Number, "Zbapi_Iport_Sd_Proc_Cash_Sales", dr.Type, dr.Id, dr.Number, dr.Message, dr.Log_No, dr.Log_Msg_No, dr.Message_V1, dr.Message_V2, dr.Message_V3, dr.Message_V4, conn, tran) < 1) Then
                                condition = False
                            End If
                        Next
                        If (condition) Then
                            Dim Credit_limit As String = ""
                            If (chkcredit) Then
                                Credit_limit = "Y"
                            Else
                                Credit_limit = "N"
                            End If
                            If (clsFc.InsertSaleOrder(Session("PayinBooking"), Iport_Number, BABIHEADER11.Doc_Type, BABIHEADER11.Req_Date_H, BABIHEADER11.Sold_To, BABIHEADER11.Bill_To, BABIHEADER11.Ship_Name, BABIHEADER11.Division, EX_SALES_NO11, EX_BILL_NO11, Credit_limit, EX_BUKRS11_Cash, EX_BELNR11_Cash, EX_GJAHR11_Cash, Bill_Date, conn, tran) > 0) Then
                                For Each dr As SAP_IPORT.ZBAPI_SALES_ITEMS In SALE_TABLE11_1
                                    If (dr.Material <> "") Then
                                        If (clsFc.InsertSAP_Sale_Order_Material(Iport_Number, dr.Material, dr.Plant, dr.Target_Qty, dr.Target_Qu, dr.Cond_Type, dr.Discount, dr.Amount, dr.Condvalue, conn, tran) < 1) Then
                                            condition = False
                                        End If
                                    End If
                                Next

                            Else
                                condition = False
                            End If
                        End If
                    Else
                        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('Can not be Create Sale Order ');", True)
                    End If
                Else
                    ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('Can not be Create Advanced Payment ');", True)
                End If
            End If
            If (CustomerTable11_5.Count > 0) Then
                'มี Mat ที่ใช้ Division 11
                Iport_Number = Iport_Number.Substring(0, 8) & "-" & (CInt(Iport_Number.Substring(9, 3)) + 1).ToString("000")
                ZBABI_VT11.Amount = SumAmount11_5.ToString("############.00")
                ZBABI_VT11.Vat = ((SumAmount11_5 * CDbl(ViewState("VAT"))) / 100).ToString("############.00")

                'ZBABI_BK11.Amount = (CDbl(SumAmount11_5 + CDbl(((SumAmount11_5 * CDbl(ViewState("VAT"))) / 100).ToString("############.00"))) - WHTAmount11_5).ToString("############.00")

                ZBABI_BK11.Amount = (CDbl(SumAmount11_5 + CDbl(((SumAmount11_5 * CDbl(ViewState("VAT"))) / 100).ToString("############.00")))).ToString("############.00")
                ZBABI_BK11.Gl_Account = "10302031"  'เลขบัญชีต้องดึงจาก Web service Bank
                ZBABI_BK11.Item_Text = "TR " & ddlBank.SelectedValue
                ZBABI_BK11.Value_Date = Date.Now.ToString("yyyyMMdd", New CultureInfo("en-US")) '"20100921"

                ZBABI_HD11.Bupla = "N014"
                ZBABI_HD11.Doc_Date = Date.Now.ToString("yyyyMMdd", New CultureInfo("en-US")) '"20100730"
                ZBABI_HD11.Header_Txt = "R|" & dsBooking.Tables(0).Rows(0).Item("Vehicle_Text") '"R|LEO GLORY"
                ZBABI_HD11.Pstng_Date = Date.Now.ToString("yyyyMMdd", New CultureInfo("en-US")) '"20100921"

                SAP_Data.Zbapi_Iport_Advanced_Payment(ZBABI_BK11, ZBABI_HD11, Iport_Number, BillTo, ZBABI_VT11, Ex_Belnr11, Ex_Bukrs11, Ex_Gjahr11, CustomerTable11_5, BABITable11)

                If (Ex_Belnr11 <> "") And (Ex_Bukrs11 <> "") And (Ex_Gjahr11 <> "") Then
                    'Advanced Payment ผ่าน
                    Dim Bill_To As String = ""
                    Dim Sold_To As String = ""
                    If (CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("Customer_no") = dsBooking.Tables(0).Rows(0).Item("MAgent_no")) Then
                        Bill_To = dsBooking.Tables(0).Rows(0).Item("MAgent_no")
                        Sold_To = Bill_To
                        If (dsBooking.Tables(0).Rows(0).Item("Bill_To_no") & "" <> "") Then
                            Bill_To = dsBooking.Tables(0).Rows(0).Item("Bill_To_no") & ""
                        End If
                    Else
                        Bill_To = CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("Customer_no")
                        Sold_To = Bill_To
                    End If

                    BABIHEADER11.Doc_Type = "ZSRC"
                    BABIHEADER11.Req_Date_H = Date.Now.ToString("yyyyMMdd", New CultureInfo("en-US"))
                    BABIHEADER11.Sold_To = Sold_To
                    BABIHEADER11.Bill_To = Bill_To
                    BABIHEADER11.Ship_Name = dsBooking.Tables(0).Rows(0).Item("Vehicle_Text")
                    BABIHEADER11.Division = "11"

                    SAP_Data.Zbapi_Iport_Sd_Proc_Cash_Sales(Bill_Date, "", Iport_Number, BABIONETIME11, BABIHEADER11, EX_BELNR11_Cash, EX_BILL_NO11, EX_BUKRS11_Cash, EX_GJAHR11_Cash, EX_SALES_NO11, BABITable11_Cash, SALE_TABLE11_5)

                    If (EX_SALES_NO11 <> "") Then
                        'Insert Advanced Payment

                        If (clsFc.InsertIportNumber(Iport_Number, conn, tran) > 0) Then
                            For Each dr As SAP_IPORT.BAPIRET1 In BABITable11
                                If (clsFc.InsertSAP_Table_MESSAGE(Iport_Number, "Zbapi_Iport_Advanced_Payment", dr.Type, dr.Id, dr.Number, dr.Message, dr.Log_No, dr.Log_Msg_No, dr.Message_V1, dr.Message_V2, dr.Message_V3, dr.Message_V4, conn, tran) < 1) Then
                                    condition = False
                                End If
                            Next
                            If (condition) Then
                                If (clsFc.InsertSAP_Advanced_Payment(Session("PayinBooking"), Iport_Number, ZBABI_BK11.Gl_Account, ZBABI_BK11.Item_Text, ZBABI_BK11.Value_Date, ZBABI_HD11.Bupla, ZBABI_HD11.Doc_Date, ZBABI_HD11.Header_Txt, ZBABI_HD11.Pstng_Date, ZBABI_VT11.Amount, ZBABI_VT11.Vat, Ex_Bukrs11, Ex_Gjahr11, Ex_Belnr11, WHTAmount11_5, "E", conn, tran) > 0) Then
                                    For Each dr As SAP_IPORT.ZBAPI_CUSTOMER In CustomerTable11_5
                                        If (dr.Material <> "") Then
                                            If (clsFc.InsertSAP_Advanced_Payment_Material(Iport_Number, dr.Customer, dr.Amount, dr.Item_Text, dr.Material, dr.Price, dr.Quantity, dr.Unit, dr.Wht_Code, conn, tran) < 1) Then
                                                condition = False
                                            End If
                                        End If
                                    Next
                                Else
                                    condition = False
                                End If
                            End If
                        Else
                            condition = False
                        End If

                        'Insert Cash Sale
                        Dim chkcredit As Boolean = True   ' True คือผ่าน
                        For Each dr As SAP_IPORT.BAPIRET1 In BABITable11_Cash
                            If (dr.Type = "E") And (dr.Id = "VF") And (dr.Number = "140") Then
                                chkcredit = False
                                CheckCreditLimit = False
                            End If
                            If (clsFc.InsertSAP_Table_MESSAGE(Iport_Number, "Zbapi_Iport_Sd_Proc_Cash_Sales", dr.Type, dr.Id, dr.Number, dr.Message, dr.Log_No, dr.Log_Msg_No, dr.Message_V1, dr.Message_V2, dr.Message_V3, dr.Message_V4, conn, tran) < 1) Then
                                condition = False
                            End If
                        Next
                        If (condition) Then
                            Dim Credit_limit As String = ""
                            If (chkcredit) Then
                                Credit_limit = "Y"
                            Else
                                Credit_limit = "N"
                            End If
                            If (clsFc.InsertSaleOrder(Session("PayinBooking"), Iport_Number, BABIHEADER11.Doc_Type, BABIHEADER11.Req_Date_H, BABIHEADER11.Sold_To, BABIHEADER11.Bill_To, BABIHEADER11.Ship_Name, BABIHEADER11.Division, EX_SALES_NO11, EX_BILL_NO11, Credit_limit, EX_BUKRS11_Cash, EX_BELNR11_Cash, EX_GJAHR11_Cash, Bill_Date, conn, tran) > 0) Then
                                For Each dr As SAP_IPORT.ZBAPI_SALES_ITEMS In SALE_TABLE11_5
                                    If (dr.Material <> "") Then
                                        If (clsFc.InsertSAP_Sale_Order_Material(Iport_Number, dr.Material, dr.Plant, dr.Target_Qty, dr.Target_Qu, dr.Cond_Type, dr.Discount, dr.Amount, dr.Condvalue, conn, tran) < 1) Then
                                            condition = False
                                        End If
                                    End If
                                Next

                            Else
                                condition = False
                            End If
                        End If
                    Else
                        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('Can not be Create Sale Order ');", True)
                    End If
                Else
                    ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('Can not be Create Advanced Payment ');", True)
                End If
            End If
            If (condition) Then
                If (CustomerTable12_0.Count > 0) Then
                    Iport_Number = Iport_Number.Substring(0, 8) & "-" & (CInt(Iport_Number.Substring(9, 3)) + 1).ToString("000")
                    ZBABI_VT12.Amount = SumAmount12_0.ToString("############.00")
                    ZBABI_VT12.Vat = ((SumAmount12_0 * CDbl(ViewState("VAT"))) / 100).ToString("############.00")

                    ' ZBABI_BK12.Amount = (CDbl(SumAmount12_0 + CDbl(((SumAmount12_0 * CDbl(ViewState("VAT"))) / 100).ToString("############.00"))) - WHTAmount12_0).ToString("############.00")

                    ZBABI_BK12.Amount = (CDbl(SumAmount12_0 + CDbl(((SumAmount12_0 * CDbl(ViewState("VAT"))) / 100).ToString("############.00")))).ToString("############.00")
                    ZBABI_BK12.Gl_Account = "10302031"  'เลขบัญชีต้องดึงจาก Web service Bank
                    ZBABI_BK12.Item_Text = "TR KBANK"
                    ZBABI_BK12.Value_Date = Date.Now.ToString("yyyyMMdd", New CultureInfo("en-US")) '"20100921"

                    ZBABI_HD12.Bupla = "N014"
                    ZBABI_HD12.Doc_Date = Date.Now.ToString("yyyyMMdd", New CultureInfo("en-US")) '"20100730"
                    ZBABI_HD12.Header_Txt = "R|" & dsBooking.Tables(0).Rows(0).Item("Vehicle_Text") '"R|LEO GLORY"
                    ZBABI_HD12.Pstng_Date = Date.Now.ToString("yyyyMMdd", New CultureInfo("en-US")) '"20100921"

                    SAP_Data.Zbapi_Iport_Advanced_Payment(ZBABI_BK12, ZBABI_HD12, Iport_Number, BillTo, ZBABI_VT12, Ex_Belnr12, Ex_Bukrs12, Ex_Gjahr12, CustomerTable12_0, BABITable12)

                    If (Ex_Belnr12 <> "") And (Ex_Bukrs12 <> "") And (Ex_Gjahr12 <> "") Then
                        'Advanced Payment ผ่าน
                        Dim Bill_To As String = ""
                        Dim SOld_To As String = ""
                        If (CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("Customer_no") = dsBooking.Tables(0).Rows(0).Item("MAgent_no")) Then
                            Bill_To = dsBooking.Tables(0).Rows(0).Item("MAgent_no")
                            SOld_To = Bill_To
                            If (dsBooking.Tables(0).Rows(0).Item("Bill_To_no") & "" <> "") Then
                                Bill_To = dsBooking.Tables(0).Rows(0).Item("Bill_To_no") & ""
                            End If
                        Else
                            Bill_To = CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("Customer_no")
                            SOld_To = Bill_To
                        End If

                        BABIHEADER12.Doc_Type = "ZSRC"
                        BABIHEADER12.Req_Date_H = Date.Now.ToString("yyyyMMdd", New CultureInfo("en-US"))
                        BABIHEADER12.Sold_To = SOld_To
                        BABIHEADER12.Bill_To = Bill_To
                        BABIHEADER12.Ship_Name = dsBooking.Tables(0).Rows(0).Item("Vehicle_Text")
                        BABIHEADER12.Division = "12"

                        SAP_Data.Zbapi_Iport_Sd_Proc_Cash_Sales(Bill_Date, "", Iport_Number, BABIONETIME12, BABIHEADER12, EX_BELNR12_Cash, EX_BILL_NO12, EX_BUKRS12_Cash, EX_GJAHR12_Cash, EX_SALES_NO12, BABITable12_Cash, SALE_TABLE12_0)
                        Dim xx As String = ""
                        For Each dr As SAP_IPORT.BAPIRET1 In BABITable12_Cash
                            xx &= dr.Message
                        Next
                        If (EX_SALES_NO12 <> "") Then
                            'Insert Advanced Payment

                            If (clsFc.InsertIportNumber(Iport_Number, conn, tran) > 0) Then
                                For Each dr As SAP_IPORT.BAPIRET1 In BABITable12
                                    If (clsFc.InsertSAP_Table_MESSAGE(Iport_Number, "Zbapi_Iport_Advanced_Payment", dr.Type, dr.Id, dr.Number, dr.Message, dr.Log_No, dr.Log_Msg_No, dr.Message_V1, dr.Message_V2, dr.Message_V3, dr.Message_V4, conn, tran) < 1) Then
                                        condition = False
                                    End If
                                Next
                                If (condition) Then
                                    If (clsFc.InsertSAP_Advanced_Payment(Session("PayinBooking"), Iport_Number, ZBABI_BK12.Gl_Account, ZBABI_BK12.Item_Text, ZBABI_BK12.Value_Date, ZBABI_HD12.Bupla, ZBABI_HD12.Doc_Date, ZBABI_HD12.Header_Txt, ZBABI_HD12.Pstng_Date, ZBABI_VT12.Amount, ZBABI_VT12.Vat, Ex_Bukrs12, Ex_Gjahr12, Ex_Belnr12, WHTAmount12_0, "E", conn, tran) > 0) Then
                                        For Each dr As SAP_IPORT.ZBAPI_CUSTOMER In CustomerTable12_0
                                            If (dr.Material <> "") Then
                                                If (clsFc.InsertSAP_Advanced_Payment_Material(Iport_Number, dr.Customer, dr.Amount, dr.Item_Text, dr.Material, dr.Price, dr.Quantity, dr.Unit, dr.Wht_Code, conn, tran) < 1) Then
                                                    condition = False
                                                End If
                                            End If
                                        Next
                                    Else
                                        condition = False
                                    End If
                                End If
                            Else
                                condition = False
                            End If

                            'Insert Cash Sale
                            Dim chkcredit As Boolean = True   ' True คือผ่าน
                            For Each dr As SAP_IPORT.BAPIRET1 In BABITable12_Cash
                                If (dr.Type = "E") And (dr.Id = "VF") And (dr.Number = "140") Then
                                    chkcredit = False
                                    CheckCreditLimit = False
                                End If
                                If (clsFc.InsertSAP_Table_MESSAGE(Iport_Number, "Zbapi_Iport_Sd_Proc_Cash_Sales", dr.Type, dr.Id, dr.Number, dr.Message, dr.Log_No, dr.Log_Msg_No, dr.Message_V1, dr.Message_V2, dr.Message_V3, dr.Message_V4, conn, tran) < 1) Then
                                    condition = False
                                End If
                            Next
                            If (condition) Then
                                Dim Credit_limit As String = ""
                                If (chkcredit) Then
                                    Credit_limit = "Y"
                                Else
                                    Credit_limit = "N"
                                End If
                                If (clsFc.InsertSaleOrder(Session("PayinBooking"), Iport_Number, BABIHEADER12.Doc_Type, BABIHEADER12.Req_Date_H, BABIHEADER12.Sold_To, BABIHEADER12.Bill_To, BABIHEADER12.Ship_Name, BABIHEADER12.Division, EX_SALES_NO12, EX_BILL_NO12, Credit_limit, EX_BUKRS12_Cash, EX_BELNR12_Cash, EX_GJAHR12_Cash, Bill_Date, conn, tran) > 0) Then
                                    For Each dr As SAP_IPORT.ZBAPI_SALES_ITEMS In SALE_TABLE12_0
                                        If (dr.Material <> "") Then
                                            If (clsFc.InsertSAP_Sale_Order_Material(Iport_Number, dr.Material, dr.Plant, dr.Target_Qty, dr.Target_Qu, dr.Cond_Type, dr.Discount, dr.Amount, dr.Condvalue, conn, tran) < 1) Then
                                                condition = False
                                            End If
                                        End If
                                    Next

                                Else
                                    condition = False
                                End If
                            End If
                        Else
                            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('Can not be Create Sale Order ');", True)
                        End If
                    Else
                        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('Can not be Create Advanced Payment ');", True)
                    End If
                End If
                If (CustomerTable12_1.Count > 0) Then
                    Iport_Number = Iport_Number.Substring(0, 8) & "-" & (CInt(Iport_Number.Substring(9, 3)) + 1).ToString("000")
                    ZBABI_VT12.Amount = SumAmount12_1.ToString("############.00")
                    ZBABI_VT12.Vat = ((SumAmount12_1 * CDbl(ViewState("VAT"))) / 100).ToString("############.00")

                    '  ZBABI_BK12.Amount = (CDbl(SumAmount12_1 + CDbl(((SumAmount12_1 * CDbl(ViewState("VAT"))) / 100).ToString("############.00"))) - WHTAmount12_1).ToString("############.00")

                    ZBABI_BK12.Amount = (CDbl(SumAmount12_1 + CDbl(((SumAmount12_1 * CDbl(ViewState("VAT"))) / 100).ToString("############.00")))).ToString("############.00")
                    ZBABI_BK12.Gl_Account = "10302031"  'เลขบัญชีต้องดึงจาก Web service Bank
                    ZBABI_BK12.Item_Text = "TR KBANK"
                    ZBABI_BK12.Value_Date = Date.Now.ToString("yyyyMMdd", New CultureInfo("en-US")) '"20100921"

                    ZBABI_HD12.Bupla = "N014"
                    ZBABI_HD12.Doc_Date = Date.Now.ToString("yyyyMMdd", New CultureInfo("en-US")) '"20100730"
                    ZBABI_HD12.Header_Txt = "R|" & dsBooking.Tables(0).Rows(0).Item("Vehicle_Text") '"R|LEO GLORY"
                    ZBABI_HD12.Pstng_Date = Date.Now.ToString("yyyyMMdd", New CultureInfo("en-US")) '"20100921"

                    SAP_Data.Zbapi_Iport_Advanced_Payment(ZBABI_BK12, ZBABI_HD12, Iport_Number, BillTo, ZBABI_VT12, Ex_Belnr12, Ex_Bukrs12, Ex_Gjahr12, CustomerTable12_1, BABITable12)

                    If (Ex_Belnr12 <> "") And (Ex_Bukrs12 <> "") And (Ex_Gjahr12 <> "") Then
                        'Advanced Payment ผ่าน
                        Dim Bill_To As String = ""
                        Dim sold_To As String = ""
                        If (CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("Customer_no") = dsBooking.Tables(0).Rows(0).Item("MAgent_no")) Then
                            Bill_To = dsBooking.Tables(0).Rows(0).Item("MAgent_no")
                            sold_To = Bill_To
                            If (dsBooking.Tables(0).Rows(0).Item("Bill_To_no") & "" <> "") Then
                                Bill_To = dsBooking.Tables(0).Rows(0).Item("Bill_To_no") & ""
                            End If
                        Else
                            Bill_To = CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("Customer_no")
                            sold_To = Bill_To
                        End If

                        BABIHEADER12.Doc_Type = "ZSRC"
                        BABIHEADER12.Req_Date_H = Date.Now.ToString("yyyyMMdd", New CultureInfo("en-US"))
                        BABIHEADER12.Sold_To = sold_To
                        BABIHEADER12.Bill_To = Bill_To
                        BABIHEADER12.Ship_Name = dsBooking.Tables(0).Rows(0).Item("Vehicle_Text")
                        BABIHEADER12.Division = "12"

                        SAP_Data.Zbapi_Iport_Sd_Proc_Cash_Sales(Bill_Date, "", Iport_Number, BABIONETIME12, BABIHEADER12, EX_BELNR12_Cash, EX_BILL_NO12, EX_BUKRS12_Cash, EX_GJAHR12_Cash, EX_SALES_NO12, BABITable12_Cash, SALE_TABLE12_1)
                        Dim xx As String = ""
                        For Each dr As SAP_IPORT.BAPIRET1 In BABITable12_Cash
                            xx &= dr.Message
                        Next
                        If (EX_SALES_NO12 <> "") Then
                            'Insert Advanced Payment

                            If (clsFc.InsertIportNumber(Iport_Number, conn, tran) > 0) Then
                                For Each dr As SAP_IPORT.BAPIRET1 In BABITable12
                                    If (clsFc.InsertSAP_Table_MESSAGE(Iport_Number, "Zbapi_Iport_Advanced_Payment", dr.Type, dr.Id, dr.Number, dr.Message, dr.Log_No, dr.Log_Msg_No, dr.Message_V1, dr.Message_V2, dr.Message_V3, dr.Message_V4, conn, tran) < 1) Then
                                        condition = False
                                    End If
                                Next
                                If (condition) Then
                                    If (clsFc.InsertSAP_Advanced_Payment(Session("PayinBooking"), Iport_Number, ZBABI_BK12.Gl_Account, ZBABI_BK12.Item_Text, ZBABI_BK12.Value_Date, ZBABI_HD12.Bupla, ZBABI_HD12.Doc_Date, ZBABI_HD12.Header_Txt, ZBABI_HD12.Pstng_Date, ZBABI_VT12.Amount, ZBABI_VT12.Vat, Ex_Bukrs12, Ex_Gjahr12, Ex_Belnr12, WHTAmount12_1, "E", conn, tran) > 0) Then
                                        For Each dr As SAP_IPORT.ZBAPI_CUSTOMER In CustomerTable12_1
                                            If (dr.Material <> "") Then
                                                If (clsFc.InsertSAP_Advanced_Payment_Material(Iport_Number, dr.Customer, dr.Amount, dr.Item_Text, dr.Material, dr.Price, dr.Quantity, dr.Unit, dr.Wht_Code, conn, tran) < 1) Then
                                                    condition = False
                                                End If
                                            End If
                                        Next
                                    Else
                                        condition = False
                                    End If
                                End If
                            Else
                                condition = False
                            End If

                            'Insert Cash Sale
                            Dim chkcredit As Boolean = True   ' True คือผ่าน
                            For Each dr As SAP_IPORT.BAPIRET1 In BABITable12_Cash
                                If (dr.Type = "E") And (dr.Id = "VF") And (dr.Number = "140") Then
                                    chkcredit = False
                                    CheckCreditLimit = False
                                End If
                                If (clsFc.InsertSAP_Table_MESSAGE(Iport_Number, "Zbapi_Iport_Sd_Proc_Cash_Sales", dr.Type, dr.Id, dr.Number, dr.Message, dr.Log_No, dr.Log_Msg_No, dr.Message_V1, dr.Message_V2, dr.Message_V3, dr.Message_V4, conn, tran) < 1) Then
                                    condition = False
                                End If
                            Next
                            If (condition) Then
                                Dim Credit_limit As String = ""
                                If (chkcredit) Then
                                    Credit_limit = "Y"
                                Else
                                    Credit_limit = "N"
                                End If
                                If (clsFc.InsertSaleOrder(Session("PayinBooking"), Iport_Number, BABIHEADER12.Doc_Type, BABIHEADER12.Req_Date_H, BABIHEADER12.Sold_To, BABIHEADER12.Bill_To, BABIHEADER12.Ship_Name, BABIHEADER12.Division, EX_SALES_NO12, EX_BILL_NO12, Credit_limit, EX_BUKRS12_Cash, EX_BELNR12_Cash, EX_GJAHR12_Cash, Bill_Date, conn, tran) > 0) Then
                                    For Each dr As SAP_IPORT.ZBAPI_SALES_ITEMS In SALE_TABLE12_1
                                        If (dr.Material <> "") Then
                                            If (clsFc.InsertSAP_Sale_Order_Material(Iport_Number, dr.Material, dr.Plant, dr.Target_Qty, dr.Target_Qu, dr.Cond_Type, dr.Discount, dr.Amount, dr.Condvalue, conn, tran) < 1) Then
                                                condition = False
                                            End If
                                        End If
                                    Next

                                Else
                                    condition = False
                                End If
                            End If
                        Else
                            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('Can not be Create Sale Order ');", True)
                        End If
                    Else
                        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('Can not be Create Advanced Payment ');", True)
                    End If
                End If
                If (CustomerTable12_5.Count > 0) Then
                    Iport_Number = Iport_Number.Substring(0, 8) & "-" & (CInt(Iport_Number.Substring(9, 3)) + 1).ToString("000")
                    ZBABI_VT12.Amount = SumAmount12_5.ToString("############.00")
                    ZBABI_VT12.Vat = ((SumAmount12_5 * CDbl(ViewState("VAT"))) / 100).ToString("############.00")

                    ' ZBABI_BK12.Amount = (CDbl(SumAmount12_5 + CDbl(((SumAmount12_5 * CDbl(ViewState("VAT"))) / 100).ToString("############.00"))) - WHTAmount12_5).ToString("############.00")

                    ZBABI_BK12.Amount = (CDbl(SumAmount12_5 + CDbl(((SumAmount12_5 * CDbl(ViewState("VAT"))) / 100).ToString("############.00")))).ToString("############.00")
                    ZBABI_BK12.Gl_Account = "10302031"  'เลขบัญชีต้องดึงจาก Web service Bank
                    ZBABI_BK12.Item_Text = "TR KBANK"
                    ZBABI_BK12.Value_Date = Date.Now.ToString("yyyyMMdd", New CultureInfo("en-US")) '"20100921"

                    ZBABI_HD12.Bupla = "N014"
                    ZBABI_HD12.Doc_Date = Date.Now.ToString("yyyyMMdd", New CultureInfo("en-US")) '"20100730"
                    ZBABI_HD12.Header_Txt = "R|" & dsBooking.Tables(0).Rows(0).Item("Vehicle_Text") '"R|LEO GLORY"
                    ZBABI_HD12.Pstng_Date = Date.Now.ToString("yyyyMMdd", New CultureInfo("en-US")) '"20100921"

                    SAP_Data.Zbapi_Iport_Advanced_Payment(ZBABI_BK12, ZBABI_HD12, Iport_Number, BillTo, ZBABI_VT12, Ex_Belnr12, Ex_Bukrs12, Ex_Gjahr12, CustomerTable12_5, BABITable12)
                    Dim xx As String = ""
                    For Each dr As SAP_IPORT.BAPIRET1 In BABITable12
                        xx &= dr.Message
                    Next
                    If (Ex_Belnr12 <> "") And (Ex_Bukrs12 <> "") And (Ex_Gjahr12 <> "") Then
                        'Advanced Payment ผ่าน
                        Dim Bill_To As String = ""
                        Dim SOld_To As String = ""
                        If (CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("Customer_no") = dsBooking.Tables(0).Rows(0).Item("MAgent_no")) Then
                            Bill_To = dsBooking.Tables(0).Rows(0).Item("MAgent_no")
                            SOld_To = Bill_To
                            If (dsBooking.Tables(0).Rows(0).Item("Bill_To_no") & "" <> "") Then
                                Bill_To = dsBooking.Tables(0).Rows(0).Item("Bill_To_no") & ""
                            End If
                        Else
                            Bill_To = CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("Customer_no")
                            SOld_To = Bill_To
                        End If

                        BABIHEADER12.Doc_Type = "ZSRC"
                        BABIHEADER12.Req_Date_H = Date.Now.ToString("yyyyMMdd", New CultureInfo("en-US"))
                        BABIHEADER12.Sold_To = SOld_To
                        BABIHEADER12.Bill_To = Bill_To
                        BABIHEADER12.Ship_Name = dsBooking.Tables(0).Rows(0).Item("Vehicle_Text")
                        BABIHEADER12.Division = "12"

                        SAP_Data.Zbapi_Iport_Sd_Proc_Cash_Sales(Bill_Date, "", Iport_Number, BABIONETIME12, BABIHEADER12, EX_BELNR12_Cash, EX_BILL_NO12, EX_BUKRS12_Cash, EX_GJAHR12_Cash, EX_SALES_NO12, BABITable12_Cash, SALE_TABLE12_5)
                        For Each dr As SAP_IPORT.BAPIRET1 In BABITable12_Cash
                            xx &= dr.Message
                        Next
                        If (EX_SALES_NO12 <> "") Then
                            'Insert Advanced Payment

                            If (clsFc.InsertIportNumber(Iport_Number, conn, tran) > 0) Then
                                For Each dr As SAP_IPORT.BAPIRET1 In BABITable12
                                    If (clsFc.InsertSAP_Table_MESSAGE(Iport_Number, "Zbapi_Iport_Advanced_Payment", dr.Type, dr.Id, dr.Number, dr.Message, dr.Log_No, dr.Log_Msg_No, dr.Message_V1, dr.Message_V2, dr.Message_V3, dr.Message_V4, conn, tran) < 1) Then
                                        condition = False
                                    End If
                                Next
                                If (condition) Then
                                    If (clsFc.InsertSAP_Advanced_Payment(Session("PayinBooking"), Iport_Number, ZBABI_BK12.Gl_Account, ZBABI_BK12.Item_Text, ZBABI_BK12.Value_Date, ZBABI_HD12.Bupla, ZBABI_HD12.Doc_Date, ZBABI_HD12.Header_Txt, ZBABI_HD12.Pstng_Date, ZBABI_VT12.Amount, ZBABI_VT12.Vat, Ex_Bukrs12, Ex_Gjahr12, Ex_Belnr12, WHTAmount12_5, "E", conn, tran) > 0) Then
                                        For Each dr As SAP_IPORT.ZBAPI_CUSTOMER In CustomerTable12_5
                                            If (dr.Material <> "") Then
                                                If (clsFc.InsertSAP_Advanced_Payment_Material(Iport_Number, dr.Customer, dr.Amount, dr.Item_Text, dr.Material, dr.Price, dr.Quantity, dr.Unit, dr.Wht_Code, conn, tran) < 1) Then
                                                    condition = False
                                                End If
                                            End If
                                        Next
                                    Else
                                        condition = False
                                    End If
                                End If
                            Else
                                condition = False
                            End If

                            'Insert Cash Sale
                            Dim chkcredit As Boolean = True   ' True คือผ่าน
                            For Each dr As SAP_IPORT.BAPIRET1 In BABITable12_Cash
                                If (dr.Type = "E") And (dr.Id = "VF") And (dr.Number = "140") Then
                                    chkcredit = False
                                    CheckCreditLimit = False
                                End If
                                If (clsFc.InsertSAP_Table_MESSAGE(Iport_Number, "Zbapi_Iport_Sd_Proc_Cash_Sales", dr.Type, dr.Id, dr.Number, dr.Message, dr.Log_No, dr.Log_Msg_No, dr.Message_V1, dr.Message_V2, dr.Message_V3, dr.Message_V4, conn, tran) < 1) Then
                                    condition = False
                                End If
                            Next
                            If (condition) Then
                                Dim Credit_limit As String = ""
                                If (chkcredit) Then
                                    Credit_limit = "Y"
                                Else
                                    Credit_limit = "N"
                                End If
                                If (clsFc.InsertSaleOrder(Session("PayinBooking"), Iport_Number, BABIHEADER12.Doc_Type, BABIHEADER12.Req_Date_H, BABIHEADER12.Sold_To, BABIHEADER12.Bill_To, BABIHEADER12.Ship_Name, BABIHEADER12.Division, EX_SALES_NO12, EX_BILL_NO12, Credit_limit, EX_BUKRS12_Cash, EX_BELNR12_Cash, EX_GJAHR12_Cash, Bill_Date, conn, tran) > 0) Then
                                    For Each dr As SAP_IPORT.ZBAPI_SALES_ITEMS In SALE_TABLE12_5
                                        If (dr.Material <> "") Then
                                            If (clsFc.InsertSAP_Sale_Order_Material(Iport_Number, dr.Material, dr.Plant, dr.Target_Qty, dr.Target_Qu, dr.Cond_Type, dr.Discount, dr.Amount, dr.Condvalue, conn, tran) < 1) Then
                                                condition = False
                                            End If
                                        End If
                                    Next

                                Else
                                    condition = False
                                End If
                            End If
                        Else
                            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('Can not be Create Sale Order ');", True)
                        End If
                    Else
                        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('Can not be Create Advanced Payment ');", True)
                    End If
                End If
            End If

            If (condition) Then
                clsDB.CommitTran(tran)
                'If (CheckCreditLimit) Then
                '    ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('ทำการสร้าง  Sale Order เรียบร้อย '); parent.closeModal('X');", True)
                'Else
                '    ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('Document cannot be billed due to credit limit check '); parent.closeModal('X');", True)
                'End If
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('ชำระเงินเรียบร้อย ');", True)
                PanelPayment.Visible = False
                lbBookingNo.Text = Session("PayinBooking")
                lbTracking.Text = tracking
                Dim rvNo As String = ""
                If Ex_Belnr11 <> "" Then
                    rvNo = Ex_Belnr11
                End If
                If (Ex_Belnr12 <> "") Then
                    If (rvNo <> "") Then
                        rvNo &= "<br />" & Ex_Belnr12
                    Else
                        rvNo = Ex_Belnr12
                    End If
                End If
                lbRVno.Text = rvNo
                lbMOney.Text = ViewState("Amt").ToString()
                PanelDetail.Visible = True

            Else
                clsDB.RollBackTran(tran)
            End If
            Application.UnLock()
            conn.Dispose()
            ClassSAP.SAP_CLOSE(SAP_Data)

            '*************************************************************************************


            Session("PayinBooking") = Nothing
        End If
    End Sub

    Private Function InsertPayment_Bank(ByVal tracking As String, ByVal Booking_no As String, ByVal Customer As String, ByVal PaymentAmt As String, ByVal BankAccount As String, ByVal Date_Time As String, ByVal Status As String, ByVal Detail As String, ByVal Bank As String) As Boolean
        Dim clsDB As New ClassDatabase()
        Dim conn As New System.Data.SqlClient.SqlConnection(connString)
        Dim condition As Boolean = True
        Dim tran As System.Data.SqlClient.SqlTransaction = Nothing
        Application.Lock()
        conn.Open()
        tran = clsDB.BeginTran(conn)
        If (clsFc.UpdatePayment_Bank(tracking, Status, Detail, conn, tran) = 0) Then
            If (clsFc.InsertPayment_Bank(tracking, Booking_no, Customer, PaymentAmt, BankAccount, Date_Time, Status, Detail, Bank, conn, tran) < 1) Then
                condition = False
            End If
        End If

        If (condition) Then
            clsDB.CommitTran(tran)
        Else
            clsDB.RollBackTran(tran)
        End If
        Application.UnLock()
        conn.Dispose()
        Return condition
    End Function

    Private Function UpdateStatusPayin() As Boolean
        Dim clsDB As New ClassDatabase()
        Dim conn As New System.Data.SqlClient.SqlConnection(connString)
        Dim condition As Boolean = True
        Dim tran As System.Data.SqlClient.SqlTransaction = Nothing
        Application.Lock()
        conn.Open()
        tran = clsDB.BeginTran(conn)
        If (clsFc.UpdateStatus_Pay(Session("PayinBooking"), conn, tran) < 1) Then
            condition = False
        End If
        If (condition) Then
            clsDB.CommitTran(tran)
        Else
            clsDB.RollBackTran(tran)
        End If
        Application.UnLock()
        conn.Dispose()
        Return condition
    End Function

    Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Session("PayinBooking") = Nothing
        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " parent.closeModal('X');", True)
    End Sub

    Private Function Gen_Report(ByVal HeadReport As String, ByVal SoldTo As String, ByVal Shipname As String, ByVal Product As String, ByVal DWT As String, ByVal LOA As String, ByVal TotalProduct As String, ByVal DF As String, ByVal Body As String, ByVal GRT As String) As String
        Dim HTML As String = ""

        HTML = "<table width='100%' border='0' cellpadding='0' cellspacing='1'>"
        HTML &= "<tr ><td colspan='6'>" & HeadReport & "</td></tr>"
        HTML &= "<tr ><td style='height:3px;' colspan='6'><img src='../Image/space.gif' /></td></tr>"

        HTML &= "<tr ><td colspan='6'><table width='100%' border='0' cellpadding='0' cellspacing='1'>"
        HTML &= "<tr>"
        HTML &= "<td style='width:140px;' align='left'><font class='White-Style' style='color:#000000'>เรียนผู้จัดการบริษัท :</font>&nbsp;</td>"
        HTML &= "<td style='width:460px;' colspan='4' align='left'><font class='White-Style' style='color:#000000'>" & SoldTo & "</font></td>"
        HTML &= "<td align='right'><font class='White-Style' style='color:#000000'>" & Date.Now.ToString("dd-MMM-") & Date.Now.ToString("yyyy", New System.Globalization.CultureInfo("en-US")) & "</font></td>"
        HTML &= "</tr>"

        HTML &= "<tr>"
        HTML &= "<td style='width:140px;' align='left'><font class='White-Style' style='color:#000000'>Ship Name :</font>&nbsp;</td>"
        HTML &= "<td style='width:120px;' align='left'><font class='White-Style' style='color:#000000'>" & Shipname & "</font></td>"
        HTML &= "<td style='width:120px;' align='left'><font class='White-Style' style='color:#000000'>Product :</font>&nbsp;</td>"
        HTML &= "<td style='width:140px;' align='left'><font class='White-Style' style='color:#000000'>" & Product & "</font></td>"
        HTML &= "<td style='width:80px;' align='left'><font class='White-Style' style='color:#000000'>DWT :</font>&nbsp;</td>"
        HTML &= "<td align='left'><font class='White-Style' style='color:#000000'>" & FormatNumber(DWT, 2) & "</font></td>"
        HTML &= "</tr>"

        HTML &= "<tr>"
        HTML &= "<td style='width:140px;' align='left'><font class='White-Style' style='color:#000000'>L.O.A(Ft.) :</font>&nbsp;</td>"
        HTML &= "<td style='width:120px;' align='left'><font class='White-Style' style='color:#000000'>" & LOA & "</font></td>"
        HTML &= "<td style='width:120px;' align='left'><font class='White-Style' style='color:#000000'>Total Product(MT) :</font>&nbsp;</td>"
        HTML &= "<td style='width:140px;' align='left'><font class='White-Style' style='color:#000000'>" & FormatNumber(TotalProduct, 3) & "</font></td>"
        HTML &= "<td style='width:80px;' align='left'>&nbsp;</td>"
        HTML &= "<td align='left'><font class='White-Style' style='color:#000000'>" & FormatNumber(GRT, 2) & " DF(Ft.) " & FormatNumber(DF, 2) & "</font></td>"
        HTML &= "</tr>"
        HTML &= "</table></td></tr>"

        HTML &= "<tr ><td style='height:3px;' colspan='6'><img src='../Image/space.gif' /></td></tr>"

        HTML &= "<tr ><td colspan='6'>" & Body & "</td></tr>"

        HTML &= "<tr ><td style='height:3px;' colspan='6'><img src='../Image/space.gif' /></td></tr>"
        Return HTML
    End Function

    Private Function Gen_Body(ByVal Vat As String, ByVal DSItem As DataTable) As String
        Dim HTML As String = ""
        HTML = "<table width='100%'  style='border-width: 0px;' border='1' cellpadding='0' cellspacing='0'>"
        Dim Value As Double = 0
        Dim ValueVat As Double = 0
        Dim WHT As Double = 0
        Dim row As Integer = 2
        For Each dr As DataRow In DSItem.Rows
            If (CDbl(dr.Item("data4")) > 0.0) Then
                Dim MatName As String = dr.Item("MAT_NAME") & "<BR /> - " & dr.Item("name")
                If (dr.Item("MAT_NAME") = dr.Item("name")) Then
                    MatName = dr.Item("MAT_NAME")
                End If
                If (row Mod 2) Then
                    HTML &= "<tr>"
                Else
                    HTML &= "<tr style='background-color:#F0F9FB;'>"
                End If
                row += 1
                Dim data1 As String = dr.Item("data1")
                Dim data2 As String = dr.Item("data2")
                Dim data3 As String = dr.Item("data3")
                Dim data4 As String = dr.Item("data4")
                FormatN(data1)
                FormatN(data2)
                FormatN(data3)
                FormatN(data4)

                HTML &= "<td style='width:140px;' valign='top'  align='left'><font class='White-Style' style='color:#000000'>" & dr.Item("MATERIAL") & "</font>&nbsp;</td>"
                HTML &= "<td style='width:280px;' align='left'><font class='White-Style' style='color:#000000'>" & MatName & "</font></td>"
                HTML &= "<td style='width:80px;' valign='bottom' align='right'><font class='White-Style' style='color:#000000'>" & data1 & "</font>&nbsp;</td>"
                HTML &= "<td style='width:80px;' valign='bottom' align='right'><font class='White-Style' style='color:#000000'>" & data2 & "</font></td>"
                HTML &= "<td style='width:70px;' valign='bottom' align='center'><font class='White-Style' style='color:#000000'>" & data3 & "</font>&nbsp;</td>"
                HTML &= "<td align='right'  valign='bottom'><font class='White-Style' style='color:#000000'>" & data4 & "</font></td>"
                HTML &= "</tr>"
                Value = Value + CDbl(dr.Item("data4"))
                Dim dt As DataTable = clsGet.Get_MeterailWhithodingTax(dr.Item("MATERIAL"))
                If (dt.Rows(0).Item("MAT_WHT") & "" = "01") Or (dt.Rows(0).Item("MAT_WHT") & "" = "02") Or (dt.Rows(0).Item("MAT_WHT") & "" = "03") Then
                    '1%
                    WHT = (WHT + CDbl(((CDbl(dr.Item("data4")) * 1) / 100).ToString("#########0.00"))).ToString("###############0.00")
                ElseIf (dt.Rows(0).Item("MAT_WHT") & "" = "04") Or (dt.Rows(0).Item("MAT_WHT") & "" = "05") Then
                    '3%
                    WHT = (WHT + CDbl(((CDbl(dr.Item("data4")) * 3) / 100).ToString("#########0.00"))).ToString("###############0.00")
                ElseIf (dt.Rows(0).Item("MAT_WHT") & "" = "06") Or (dt.Rows(0).Item("MAT_WHT") & "" = "22") Then
                    '5%
                    WHT = (WHT + CDbl(((CDbl(dr.Item("data4")) * 5) / 100).ToString("#########0.00"))).ToString("###############0.00")
                End If
            End If

        Next


        HTML &= "<tr>"
        HTML &= "<td style='border-width: 1px 0px 0px 0px;' colspan='5' align='left'>&nbsp;</td>"
        HTML &= "<td align='right'><font class='White-Style' style='color:#000000'>" & FormatNumber(Value.ToString("#############0.00")) & "</font></td>"
        HTML &= "</tr>"

        ValueVat = ((Value * CDbl(Vat)) / 100).ToString("############.00")
        HTML &= "<tr>"
        HTML &= "<td style='border-width: 0px;' colspan='5' align='right'><font class='White-Style' style='color:#000000'>Vat " & Vat & "%</font>&nbsp;</td>"
        HTML &= "<td align='right'><font class='White-Style' style='color:#000000'>" & FormatNumber(ValueVat) & "</font></td>"
        HTML &= "</tr>"

        HTML &= "<tr>"
        HTML &= "<td style='border-width: 0px;' colspan='5' align='right'><font class='White-Style' style='color:#000000'>รวมประเมินค่าใช้จ่าย</font>&nbsp;</td>"
        HTML &= "<td align='right'><font class='White-Style' style='color:#000000'>" & FormatNumber((Value + ValueVat).ToString("###############.00")) & "</font></td>"
        HTML &= "</tr>"

        HTML &= "</table>"

        HTML &= Gen_Sum(Value.ToString("#############0.00"), ValueVat.ToString(), WHT.ToString())
        Return HTML
    End Function
    Private Function Gen_Sum(ByVal Net As String, ByVal Vat As String, ByVal WHT As String) As String
        Dim HTML As String = ""

        HTML = "<table width='100%' border='0' cellpadding='0' cellspacing='1'>"

        HTML &= "<tr><td colspan='6'>&nbsp;</td></tr>"

        HTML &= "<tr>"
        HTML &= "<td>&nbsp;</td>"
        HTML &= "<td style='width:130px; background-color:#aeaeae;' align='center'><font class='White-Style' style='color:#000000'>NET</font></td>"
        HTML &= "<td style='width:130px; background-color:#aeaeae;' align='center'><font class='White-Style' style='color:#000000'>VAT</font></td>"
        HTML &= "<td style='width:130px; background-color:#aeaeae;' align='center'><font class='White-Style' style='color:#000000'>WHITHOLDING</font>&nbsp;</td>"
        HTML &= "<td style='width:130px; background-color:#aeaeae;' align='center'><font class='White-Style' style='color:#000000'>ยอดเงินที่ต้องชำระ</font></td>"
        HTML &= "<td>&nbsp;</td>"
        HTML &= "</tr>"
        ViewState("Amt") = FormatNumber(((CDbl(Net) + CDbl(Vat)) - CDbl(WHT)).ToString("#############0.00"), 2)
        HTML &= "<tr>"
        HTML &= "<td>&nbsp;</td>"
        HTML &= "<td style='width:130px; background-color:#e7f9ff;' align='center'><font class='White-Style' style='color:#000000'>" & FormatNumber(Net, 2) & "</font></td>"
        HTML &= "<td style='width:130px; background-color:#e7f9ff;' align='center'><font class='White-Style' style='color:#000000'>" & FormatNumber(Vat, 2) & "</font></td>"
        HTML &= "<td style='width:130px; background-color:#e7f9ff;' align='center'><font class='White-Style' style='color:#000000'>" & FormatNumber(WHT, 2) & "</font>&nbsp;</td>"
        HTML &= "<td style='width:130px; background-color:#e7f9ff;' align='center'><font class='White-Style' style='color:#000000'>" & ViewState("Amt") & "</font></td>"
        HTML &= "<td>&nbsp;</td>"
        HTML &= "</tr>"

        HTML &= "</table>"
        Return HTML
    End Function

    Private Sub FormatN(ByRef data As String)
        Try
            data = FormatNumber(data, 2)
        Catch ex As Exception

        End Try
    End Sub

    Private Function PaymentBank(ByVal tracking As String, ByVal CustomerCode As String, ByVal PaymentAmt As String, ByVal BankAccount As String, ByRef Status As String, ByRef Detail As String, ByVal Request As String) As Boolean
        Dim ws As New WS_BANK.Service
        Dim Header As New WS_BANK.PaymentRequestHeader
        Header.trackingNumber = tracking
        Header.sendDate = Date.Now.ToString("yyyyMMdd", New CultureInfo("en-us"))   'วันที่ส่ง
        Header.sendTime = Date.Now.ToString("HHmmss")    'เวลาที่ส่ง
        Header.sponsorCode = "IRPC3"   'IRPC3 = PORT
        Header.customerCode = CustomerCode   ' Customer Code
        Header.bankCode = "004"    '004 = K-Bank
        Header.totalInvoice = 0
        Header.paymentAmt = PaymentAmt
        Header.paymentAmtFlag = "1"  'จำนวนเงินรวม  0=ค่าลบ   1=ค่าบวก
        Header.paymentDate = Date.Now.ToString("yyyyMMdd", New CultureInfo("en-us"))  'วันปัจจุบัน
        Header.paymentType = "1"  '0= ชำระโดยขอสินเชื่อ   1= ตัดบัญชี
        Header.paymentDetail = BankAccount   'หมายเลขบัญชีธนาคารทีNส่งมาตัดเงินจะตอ้ งเป็นหมายเลขบัญชีทมีN ีอยใู่ นระบบK-Supply Chain

        Dim Invoice(0) As WS_BANK.Invoice
        'Invoice(0) = New WS_BANK.Invoice
        'Invoice(0).documentType = "1"
        'Invoice(0).documentNumber = "000002"
        'Invoice(0).documentDate = Date.Now.ToString("yyyyMMdd", New CultureInfo("en-us"))
        'Invoice(0).dueDate = Date.Now.ToString("yyyyMMdd", New CultureInfo("en-us"))
        'Invoice(0).documentAmt = 0
        'Invoice(0).documentAmtFlag = "0"
        'Invoice(0).outstandingAmt = 0
        'Invoice(0).outstandingAmtFlag = "0"

        Dim payment As New WS_BANK.PaymentRequestData()

        payment.header = Header

        Dim RQ As New WS_BANK.PaymentRequestMessage()
        'PAYMENT_REQUEST  , PAYMENT_INQUIRY
        RQ.operation = Request
        RQ.paymentRequestData = payment

        Dim RS As New WS_BANK.PaymentResponseMessage

        ws.Url = URLwebService
        Try
            RS = ws.InvokePayment("irpc_escf", "pwd12345", URLwebServiceBank & "PaymentService?wsdl", RQ, "w.ongarj", "Kun123456", "go2net2.irpc.in.th", 8080)
            Status = RS.paymentResponseData.responseCode.Trim()
            Detail = RS.paymentResponseData.responseDetail
        Catch ex As Exception
            RQ.operation = "PAYMENT_INQUIRY"

            RS = ws.InvokePayment("irpc_escf", "pwd12345", URLwebServiceBank & "PaymentService?wsdl", RQ, "w.ongarj", "Kun123456", "go2net2.irpc.in.th", 8080)

            Status = RS.paymentResponseData.responseCode.Trim()
            Detail = RS.paymentResponseData.responseDetail
            If (RS.paymentResponseData.responseCode.Trim() = "D") Then
                Return True
            End If
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('" & RS.paymentResponseData.responseDetail & "');", True)
            Return False
        End Try


        If (RS.paymentResponseData.responseCode.Trim() = "D") Then
            Return True
        Else
            If (RS.paymentResponseData.responseCode.Trim() = "ED") Then
                RQ.operation = "PAYMENT_INQUIRY"

                RS = ws.InvokePayment("irpc_escf", "pwd12345", URLwebServiceBank & "PaymentService?wsdl", RQ, "w.ongarj", "Kun123456", "go2net2.irpc.in.th", 8080)
                Status = RS.paymentResponseData.responseCode.Trim()
                Detail = RS.paymentResponseData.responseDetail
                If (RS.paymentResponseData.responseCode.Trim() = "D") Then
                    Return True
                End If
            End If
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('" & RS.paymentResponseData.responseDetail & "');", True)
            Return False

        End If
    End Function

    Private Function GetBookingAccount(ByVal CustomerCode As String) As DataTable
        Dim ws As New WS_BANK.Service
        Dim Header As New WS_BANK.BankAccRequestHeader
        Dim bankAccount As New WS_BANK.BankAccRequestData()
        Dim RQ As New WS_BANK.BankAccRequestMessage()
        Dim RC(0) As WS_BANK.BankAccRequestCustomer

        Header.sponsorCode = "IRPC3"
        Header.bankCode = "004"
        Header.totalCustomerCode = 1

        RC(0) = New WS_BANK.BankAccRequestCustomer
        RC(0).customerCode = CustomerCode

        bankAccount.bankAccRequestHeader = Header
        bankAccount.bankAccRequestCustomer = RC

        RQ.operation = "BANK_ACCOUNT"
        RQ.bankAccRequestData = bankAccount

        Dim RS As New WS_BANK.BankAccResponseMessage
        Dim dt As New DataTable
        dt.Columns.Add("Account", GetType(String))
        dt.AcceptChanges()

        Try
            RS = ws.InvokeBooking("irpc_escf", "pwd12345", "https://203.147.52.228/ESCFWS/services/BankAccountService?wsdl", RQ, "w.ongarj", "Kun123456", "go2net2.irpc.in.th", 8080)

            If Not (RS.bankAccResponseData.bankAccResponseCustomer(0).accounts Is Nothing) Then
                For i As Integer = 0 To RS.bankAccResponseData.bankAccResponseCustomer(0).accounts.Length - 1
                    If (RS.bankAccResponseData.bankAccResponseCustomer(0).accounts(i).bankAccountNo <> "") Then
                        Dim dr As DataRow = dt.NewRow
                        dr.Item("Account") = RS.bankAccResponseData.bankAccResponseCustomer(0).accounts(i).bankAccountNo
                        dt.Rows.Add(dr)
                        dt.AcceptChanges()
                    End If
                Next
            End If
        Catch ex As Exception
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "  alert('ไม่สามารถทำการเชื่อมต่อธนาคารได้ !!');", True)
        End Try

        Return dt
    End Function

    Private Sub ddlBank_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlBank.SelectedIndexChanged
        If (ddlBank.SelectedValue & "" = "KBANK") Then
            With ddlAccount
                .Items.Clear()
                .ClearSelection()
                .DataTextField = "Account"
                .DataValueField = "Account"
                Dim customer As Integer = 0
                Try
                    customer = CInt(CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("Customer_no"))
                Catch ex As Exception
                End Try
                Dim dt As DataTable = GetBookingAccount(customer)
                .DataSource = dt
                .DataBind()
                If dt.Rows.Count > 0 Then
                    btnConfirm.Visible = True
                End If
            End With
        Else
            ddlAccount.Items.Clear()
            btnConfirm.Visible = False
        End If


    End Sub

    Private Sub btnCloseDetail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCloseDetail.Click
        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "  parent.closeModal('X');", True)
    End Sub


    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "")
        End If
    End Sub
End Class